package com.wlz.ota.util;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.Collections;
import java.util.List;

/**
 * @author wlz
 * @date 2022-10-31  7:23 下午
 */
@Slf4j
public class ExcelImportUtil {

    /**
     * excel 导入
     *
     * @param filePath  excel文件路径
     * @param pojoClass pojo类型
     * @param url       保存路径
     * @param <T>
     * @return
     */
    public static <T> List<T> importExcel(String filePath, Class<?> pojoClass, String url) {
        List<T> list = null;
        try {
            if (StringUtils.isBlank(filePath)) {
                return Collections.emptyList();
            }
            String uploadExcelDir = "excel/" + url;
            String fullExcelPath = CommonUtil.resolveTmpPath(uploadExcelDir);
            ImportParams params = new ImportParams();
            params.setTitleRows(0);
            params.setHeadRows(1);
            params.setSaveUrl(fullExcelPath);
            params.setNeedSave(false);
            list = cn.afterturn.easypoi.excel.ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
        } catch (Exception e) {
            log.error("导入Excel异常", e);
        }
        return list;
    }
}
